package com.victor.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.victor.system.service.SysSubHospitalService;
import com.victor.common.core.utils.StringUtils;
import com.victor.common.dao.system.SysHospitalDao;
import com.victor.common.dao.system.SysSubHospitalDao;
import com.victor.common.entity.system.SysHospital;
import com.victor.common.entity.system.SysSubHospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 子医院信息表 服务实现类
 * </p>
 *
 * @author benetech
 * @since 2022年11月23日
 */
@Service
public class SysSubHospitalServiceImpl extends ServiceImpl<SysSubHospitalDao, SysSubHospital> implements SysSubHospitalService {

    @Autowired
    private SysHospitalDao sysHospitalDao;

    @Override
    public Boolean checkHospital(SysSubHospital body) {
        Long integer = this.baseMapper.selectCount(new LambdaQueryWrapper<SysSubHospital>()
                .ne(SysSubHospital::getId,body.getId())
                .and(wrapper ->wrapper.eq(StringUtils.isNotEmpty(body.getOrgSubCode()), SysSubHospital::getOrgSubCode, body.getOrgSubCode())
                        .or()
                        .eq(StringUtils.isNotEmpty(body.getOrgSubName()), SysSubHospital::getOrgSubName, body.getOrgSubName())));
        //查询子机构编码与父机构编码不可重复
        Long integerSub = sysHospitalDao.selectCount(new LambdaQueryWrapper<SysHospital>()

                .or(wrapper -> wrapper.eq(StringUtils.isNotEmpty(body.getOrgSubCode()), SysHospital::getOrgCode, body.getOrgSubCode())
                .eq(StringUtils.isNotEmpty(body.getOrgSubName()), SysHospital::getOrgName, body.getOrgSubName())));
        return integer > 0 || integerSub > 0;
    }
}
